/**
 * 城市逾期单,单个搜索
 */
$(".cucityoverduetype").click(function(){
	ajaxcityoverdue("","cucityoverduetype");
});
$(".cutomeoverduecity").click(function(){
	ajaxcityoverdue("","cutomeoverduecity");
});
/**
 * 城市逾期订单
 * @param  {[type]} data [description]
 * @return {[type]}      [description]
 */
function ajaxcityoverdue(page,datatype){
	if(datatype == "cutomeoverduecity"){
		var start = $("#start").val();//开始月
		var end   = $("#end").val();//结束月
		var sheng = $("#sheng").val();//省
		var shi   = $("#shi").val();//市
		var startrate   = $("#startrate").val();//开始百分比
		var endrate     = $("#endrate").val();//结束百分比
		var signrate    = $("#signrate").val();//开始标记百分比
        var signendrate = $("#signendrate").val();//结束标记百分比
		// var signeradio  = $(".radio").val();//结束标记百分比
        var signeradio  = $("input[name='0']:checked").val();
        if(signeradio == "1"){
            if(!isIntNum(start) || !isIntNum(end) || end == "" || start == ""){
                sweetAlert('', '结束月不能为空或请输入数字', 'error');
                return false;
            }
            if(parseInt(start) > parseInt(end)){
                sweetAlert('','起始月不能大于结束月','error');
                return false; 
            }
            if(start == 0 || end == 0){
                sweetAlert('','起始月不能输入零','error');
                return false; 
            }
            
        }else{
            var selectvale = $(".select_time").val();
            if(selectvale != 0){
                start = selectvale;
                end   = selectvale;
            }else if(selectvale == 0){
                start = 1;
                end   = 12;
            }
        }
		if(startrate == "" && endrate == ""){
			startrate = 0;
			endrate   = 100;
		}else if(parseInt(startrate) > parseInt(endrate)){
            sweetAlert('','起始值不能大于结束值','error');
            return false;
        }
        if(!isIntNum(startrate) || !isIntNum(endrate)){
            sweetAlert('','请输入数字','error');
            return false;
        }
		data ={start:start,end:end,
			   typelv:"toorate",
			   provinceid:sheng,
			   cityid:shi,
			   startrate:startrate,
			   endrate:endrate,
			   signrate:signrate,
			   signendrate:signendrate
			  }

	}else if(datatype == "cucityoverduetype"){
		var cutomecity = $("input[name='cutomecity']").val();
		if(!cutomecity){
			sweetAlert('','城市名称不能为空','error');
	        return false;
		}
		data = {cityname:cutomecity};
	}
	ajax({
		'url':'/admin/Singlerate/OverdueOrder',
		'type':'post',
		'data':data,
		'dataType':'json',
		'success':function(response) {
            if(response.resdata && response.resdata.length !=0){
                setItem('orders', response.resdata);
                var totalPage = Math.ceil(response.resdata.length / 15);
                showPage(1, totalPage, addInfo);recovery();
                addInfo(1, totalPage);
            }else{
                addNotify();
            }
			
		}
	},true)
}

function addInfo(page,totalPage){
	var data = pageData(page,totalPage)
	if (data.length <= 0) {
        $('.table_hover tr').remove();
        return;
    }
    showPage(page,totalPage,addInfo);
    $('.table_hover tr').remove();
    $('.tr_item_title').children().remove();
    var children = $('.tr_checkbox').children();
    for (var i = 0; i < children.length; i++) {
        children[i].remove();
    }
    var th = '';
    var tr = '';
    // var months = ['当月', '上月', '第3月', '第4月', '第5月', '第6月', '第7月', '第8月', '第9月', '第10月', '第11月', '第12月'];
    tr += "<th>序号</th> <th>城市名称</th> <th>所属省份</th> <th>GW在职总数</th> <th>城市渠道激活数</th><th>CS姓名</th> ";
    tr += "<th>CS岗位号</th> <th>SZ姓名</th>  <th>SZ岗位号</th> <th>总逾期数</th> <th>总逾期比</th> <th>总逾期率</th>";
    th += "<th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th>";
    th += "<th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th>";
    var trStatus = true;
    for (var i = 0; i < data.length; i++) {
    	cityname = "";
    	citygwhn = "";
        var str = '';
        str += "<td>" + (i + 1) + "</td>";
        str += "<td>" + data[i].cityname+ "</td>";
        str += "<td>" + data[i].provanem + "</td>";
        str += "<td>" + data[i].citycount + "</td>";
        str += "<td>" + data[i].waycount + "</td>";
        str += "<td>" + data[i].name + "</td>";
        str += "<td>" + data[i].usernum + "</td>";
        str += "<td>" + data[i].szname + "</td>";
        str += "<td>" + data[i].szusernum + "</td>";
        var orders = data[i].orderarr;
        for (var j = 0; j < orders.length; j++) {
        	if(orders[j].overduecount || orders[j].overduecountorder || orders[j].overduecountrate){
        		str += "<td>" + orders[j].overduecount + "</td>";
                str += "<td>" + orders[j].overduecountorder + "</td>";
                str += "<td>" + orders[j].overduecountrate + "%</td>";
        	}
        }
        for (var j = 0; j < orders.length; j++) {
        	if(orders[j].overdue || orders[j].overdueorder || orders[j].overduerate){
        		str += "<td>" + orders[j].overdue + "</td>";
                str += "<td>" + orders[j].overdueorder + "</td>";
                str += "<td>" + orders[j].overduerate + "%</td>";
        	}
        }
        if(trStatus){
        	for (var j = data[i].start; j <= data[i].end; j++) {
        		if(j == 1){
            			months = "当月";
            		}else if(j == 2){
            			months = "上月";
            		}else{
            			months = "第"+j+"月";	
            		}
                tr += "<th>" + months+ "逾期数</th>";
                tr += "<th>" + months+ "逾期比</th>";
                tr += "<th>" + months+ "逾期率</th>";
                th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
        	}
            th+='<th calss="btn_rate selected" id="btn_filter">'+
                      '<button type="button" class="btn btn-warning btn_filter" >过滤</button>'+
                      '</th>'+
                      '<th calss="btn_rate selected" id="btn_recovery">'+
                      '<button type="button" class="btn btn_recovery" >重置</button>'+
                      '</th>';
        }
        var trStatus = false;
        var trcolor = "";
        if(data[i].typesign == "red"){
        	// $('.table_hover tr').css({'color':'red'});
        	trcolor = "color:red";
         //tr.css({'background-color':'#ffff00','color':'red'});
        }
        str += "<td class='action_list'><a href='./cityrisk.html?cutomecity="+data[i].id+"'><span>查看详情</span></a></td>";
        str += "<td></td>";
        var str2 = "<tr style="+trcolor+">" + str + "</tr>";
        $('.table_hover').append(str2);
        
    }
    tr += "<th class='action'>操作</th>";
    tr += "<th class='action'></th>";
    $('.tr_item_title').append(tr);
    $('.thead_item .tr_checkbox').append(th);
    if (!filterTag) {
        tableHide();
    }else{
        recovery();
    }
}

$(".cityoverdue").click(function(){
    var status = $(this).val();
    if(!status){
        sweetAlert('','导出程序错误','error');
        return false;
    }
    ajax({
        'url':'/admin/Singlerate/ExportExcelSeason',
        'type':'post',
        'data':{status:status,arrarray:filterIndexs},
        'dataType':'json',
        'success':function(response) {
            if(response.success == "OK"){
                location.href = response.path;

            }
        }
    },true)
});
$('.radio').click(function () {
    var value = $(this).val();
    switch(value) {
        case "0":
            $('#start').attr("disabled", "disabled");
            $('#end').attr("disabled", "disabled");
            $("#start").val("");
            $("#end").val("");
            $('.select_time').removeAttr("disabled");
            break;
        case "1":
            $('.select_time').attr("disabled", "disabled");
            $('#start').removeAttr("disabled");
            $('#end').removeAttr("disabled");
            break;
    }
});